package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.base.validation.Add;
import com.cheetah.start.common.base.validation.Edit;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.module.system.service.SysRoleAdminService;
import com.cheetah.start.module.system.service.SysRoleMenuService;
import com.cheetah.start.module.system.domain.SysRole;
import com.cheetah.start.module.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 系统角色控制器
 *
 * @author Feel
 * @date 2021/09/05
 */
@Api(tags = "角色管理crud")
@Slf4j
@Validated
@RestController
@RequestMapping("/system/role")
public class SysBaseRoleController extends ResultController {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private SysRoleAdminService roleAdminService;


    /**
     * 添加
     *
     * @param role 角色
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:add", orRole = "admin")
    @ApiOperation(value = "角色添加")
    @Log(title = "角色", describe = "添加", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    @PostMapping("/save")
    public Result save(@Validated(value = {Add.class}) SysRole role) {
        return decide(roleService.save(role), BusinessTypeEnum.ADD);
    }

    /**
     * 更新
     *
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:edit", orRole = "admin")
    @ApiOperation(value = "角色更新")
    @Log(title = "角色", describe = "更新", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    @PostMapping("/update")
    public Result update(@Validated(value = {Edit.class}) SysRole role) {
        if (roleService.isSuperAdminDisable(role)) {
            return failure("超级管理员不能禁用");
        }
        return decide(roleService.updateById(role), BusinessTypeEnum.EDIT);
    }


    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @param role      角色
     * @return {@link ResultTable}
     */
    @SaCheckPermission(value = "sys:role:data", orRole = "admin")
    @ApiOperation(value = "角色列表")
    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery, SysRole role) {
        return roleService.getPage(pageQuery, role);
    }

    /**
     * 删除
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:delete", orRole = "admin")
    @ApiOperation(value = "角色删除")
    @Log(title = "角色", describe = "删除", type = BusinessTypeEnum.REMOVE, logType = LogTypeEnum.OPERATE)
    @DeleteMapping("/deleteOne")
    public Result delete(@NotNull(message = "ID不能为空") Integer roleId) {
        if (roleService.isSuperAdmin(roleId)) {
            return failure("超级管理员不能删除");
        }
        boolean b = roleService.removeById(roleId);
        roleMenuService.removeByRoleId(roleId);
        roleAdminService.removeByRoleId(roleId);
        return decide(b, BusinessTypeEnum.REMOVE);
    }


}
